package cn.schoolwow.quickserver.handler;

import cn.schoolwow.quickserver.domain.Client;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.List;

/**
 * keepAlive处理器
 * */
public class KeepAliveHandler implements Handler{
    Logger logger = LoggerFactory.getLogger(KeepAliveHandler.class);
    @Override
    public Handler handle(Client client) throws Exception {
        List<String> connectionHeader =  client.httpRequestMeta.headers.get("Connection");
        if(null!=connectionHeader&&connectionHeader.size()>0){
            String value = connectionHeader.get(0);
            if(!"close".equalsIgnoreCase(value)&&client.httpRequestMeta.protocol.contains("1.1")){
                if(client.serverConfigMeta.keepAliveClientList.stream().noneMatch(client1 -> client1.socket==client.socket)){
                    logger.trace("[缓存客户端套接字]{}",client.socket);
                    client.serverConfigMeta.keepAliveClientList.add(client);
                }
            }
        }
        return null;
    }
}
